﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QAQ
{
    class RingPostM
    {
        public enum RingPstType
        {
            press,
            down,
            up
        };
        public static void RingPst(IntPtr hwnd, VirtualKeys vk, RingPstType keytype)
        {
            switch (keytype)
            {
                case RingPstType.press:
                    User32.PostMessageA((IntPtr)0x00d07aa, (uint)WMsg.WM_KEYDOWN, (int)vk, makeLp(vk, WMsg.WM_KEYDOWN));
                    User32.SendMessage((IntPtr)0x0004095e, (uint)WMsg.WM_KEYDOWN, vk, makeLp(vk, WMsg.WM_KEYDOWN));
                    System.Threading.Thread.Sleep(33);
                    User32.PostMessageA((IntPtr)0x00d07aa, (uint)WMsg.WM_KEYUP, (int)vk, makeLp(vk, WMsg.WM_KEYUP));
                    User32.SendMessage((IntPtr)0x0004095e, (uint)WMsg.WM_KEYUP, vk, makeLp(vk, WMsg.WM_KEYUP));
                    break;
                case RingPstType.down:
                    User32.PostMessageA(hwnd, (uint)WMsg.WM_KEYDOWN, (int)vk, makeLp(vk, WMsg.WM_KEYDOWN));
                    break;
                case RingPstType.up:
                    User32.PostMessageA(hwnd, (uint)WMsg.WM_KEYUP, (int)vk, makeLp(vk, WMsg.WM_KEYUP));
                    break;

            }

        }


        private static int makeLp(VirtualKeys vk, int wmKey)
        {
            uint scanCode = User32.MapVirtualKey((uint)vk, 0);
            uint lParam = 0;
            lParam = (0x00000001 | (scanCode << 16));
            if (wmKey != WMsg.WM_KEYDOWN)
            {
                lParam |= 0xc0000000;
            }
            return (int)lParam;
        }
    }
}
